﻿using Framework.Core.Attributes;
using Framework.Core.Enums;
using Microsoft.AspNetCore.Mvc;
using my.abp.Domain.IManagers.Dict;
using my.abp.Domain.Repositories.Dict;
using my.abp.Domain.Shared.Entities;
using my.abp.Domain.Shared.Enums;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Services;

namespace my.abp.Domain.Managers.Dict
{
	public class SysDictDataManager : DomainService,ISysDictDataManager
	{
		private readonly ISysDictDataRepository _sysDictDataRepository;

		public SysDictDataManager(ISysDictDataRepository sysDictDataRepository)
		{
			_sysDictDataRepository = sysDictDataRepository;
		}

		public async Task<List<SysDictData>> GetList(long DictTypeId, string Value, string Code, int pageNumber, int pageSize, RefAsync<int> total)
		{
			var code = !string.IsNullOrEmpty(Code?.Trim());
			var value = !string.IsNullOrEmpty(Value?.Trim());
			var queryable = await _sysDictDataRepository.AsQueryable();

			if (queryable == null)
			{
				total = 0;
				return null;
			}

			return await queryable
				.Where(u => u.DictTypeId == DictTypeId)
				.WhereIF(code, u => u.Code.Contains(Code))
				.WhereIF(value, u => u.Value.Contains(Value))
				.OrderBy(u => new { u.OrderNo, u.Code })
				.ToPageListAsync(pageNumber, pageSize, total);
		}

		public async Task<List<SysDictData>> GetDictDataListByDictTypeId(long dictTypeId)
		{
			var queryable = await _sysDictDataRepository.AsQueryable();

			return await queryable
				.Where(u => u.DictTypeId == dictTypeId)
				.OrderBy(u => new { u.OrderNo, u.Code })
				.ToListAsync();
		}

		public async Task<bool> AddDictData(SysDictData input)
		{
			var isExist = await _sysDictDataRepository.IsAnyAsync(u => u.Code == input.Code && u.DictTypeId == input.DictTypeId);
			if (isExist)
				throw new UserFriendlyException(RemarkAttribute.GetEnumRemark(ErrorCodeEnum.D3003));

			return await _sysDictDataRepository.InsertAsync(input);
		}

		public async Task UpdateDictData(SysDictData input)
		{
			var isExist = await _sysDictDataRepository.IsAnyAsync(u => u.Id == input.Id);
			if (!isExist) throw new UserFriendlyException(RemarkAttribute.GetEnumRemark(ErrorCodeEnum.D3004));

			isExist = await _sysDictDataRepository.IsAnyAsync(u => u.Code == input.Code && u.DictTypeId == input.DictTypeId && u.Id != input.Id);
			if (isExist) throw new UserFriendlyException(RemarkAttribute.GetEnumRemark(ErrorCodeEnum.D3003));

			await _sysDictDataRepository.UpdateAsync(input);
		}

		public async Task DeleteDictData(long Id)
		{
			var dictData = await _sysDictDataRepository.GetFirstAsync(u => u.Id == Id);
			if (dictData == null)
				throw new UserFriendlyException(RemarkAttribute.GetEnumRemark(ErrorCodeEnum.D3004));

			await _sysDictDataRepository.DeleteAsync(dictData);
		}

		public async Task DeleteDictDataFromDictTypeId(long dictTypeId)
		{
			await _sysDictDataRepository.DeleteAsync(u => u.DictTypeId == dictTypeId);
		}

		public async Task<SysDictData> GetDetail(long Id)
		{
			return await _sysDictDataRepository.GetFirstAsync(u => u.Id == Id);
		}

		public async Task SetStatus(long Id, StatusEnum Status)
		{
			var dictData = await _sysDictDataRepository.GetFirstAsync(u => u.Id == Id);
			if (dictData == null)
				throw new UserFriendlyException(RemarkAttribute.GetEnumRemark(ErrorCodeEnum.D3004));

			if (!System.Enum.IsDefined(typeof(StatusEnum), Status))
				throw new UserFriendlyException(RemarkAttribute.GetEnumRemark(ErrorCodeEnum.D3005));

			dictData.Status = Status;
			await _sysDictDataRepository.UpdateAsync(dictData);
		}
	}
}
